#!/usr/bin/python
# _*_ coding: utf-8 _*_

'''
要求：运用面向对象的知识，构造一个类来描述每个国家的奖牌情况。
类的属性包括：国家名、金银铜牌数量
再提供方法：新增奖牌、输出奖牌榜信息、获取奖牌总数等

分别按金牌数和奖牌总数对奖牌榜列表进行排序
'''

#Load Package
import sys,os,pickle

#Load file
Olympic_File = 'E:\stury_program\python\Olympic_Game.txt'

#Check main
if __name__ == '__main__':
    pass
else:
    print "Not main source quit this script."
    exit()

'''
高亮颜色为 '\033[32m' + string + '\033[0m'，定义颜色组方便之后的调用
'''
class color:
    HEAD = '\033[32m'
    END = '\033[0m'

class Olympic_game():
    def __init__(self, country, gold, silver, bronze):
        self.country = country
        self.gold = gold
        self.silver = silver
        self.bronze = bronze

    def in_new_medal(self):
        Go_on = True
        while Go_on:
            g_s_b = raw_input("请选择要输入的奖牌类型： 1.金牌 2.银牌 3.铜牌 \n")
            if g_s_b == "1":
                self.gold += input("请输入获取的金牌数量：")
                break
            elif g_s_b == "2":
                self.silver += input("请输入获取的银牌数量：")
                break
            elif g_s_b == "3":
                self.bronze += input("请输入获取的铜牌数量：")
                break
            else:
                Go_on = False
                print "输入错误，请重新调取"

    def sum_medal(self):
        return self.gold + self.silver + self.bronze

    def __str__(self):
        return '%s: 金 %s, 银 %s, 铜 %s, 总 %s' % (self.country, self.gold, self.silver, self.bronze, self.sum_medal())

if os.path.exists(r'%s' %Olympic_File):
    all_medal = pickle.load(file(r'%s' %Olympic_File))
    print color.HEAD+"上次奖牌榜信息为： "+color.END
    for i in all_medal:
        print i
else:
    all_medal = []

china = Olympic_game("中国", 30, 60, 30)
us = Olympic_game("美国", 50, 40, 10)
uk = Olympic_game("英国", 30, 20, 10)
#print china
#print us
#print uk

all_medal = [china, us, uk]

#更新奖牌信息
#china.in_new_medal()
#us.in_new_medal()
#uk.in_new_medal()

#按需求更新奖牌信息
temp_list = sorted(all_medal, key=lambda x:x.gold,reverse=True)
print color.HEAD + '金奖牌榜信息: ' + color.END
for i in temp_list:
    print i
temp_list = sorted(all_medal, key=lambda x:x.sum_medal(),reverse=True)
print color.HEAD + '总奖牌榜信息: ' + color.END
for i in temp_list:
    print i

# 存入pickle供下次调用
pickle.dump(all_medal,file(r'%s' %Olympic_File,'w'))